package com.studyadmin.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.studyadmin.entity.Student;
import com.studyadmin.service.StudentService;
import com.studyadmin.util.JsonUtil;
import com.studyadmin.dto.RegParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 学生表 前端控制器
 * </p>
 *
 * @author lyl
 * @since 2023-03-14
 */
@RestController
@RequestMapping("/studyadmin/student")
@CrossOrigin
public class StudentController {

    @Autowired
    StudentService studentService;

    /**
     *7772841DC6099402
     * D:\java\Tomcat\apache-tomcat-8.5.78\webapps\photos\a1.png
     * 查询学生表所有信息
     */
    @RequestMapping("/stulist")
    public JsonUtil stuList(){

        List< Student > list=studentService.list();
        JsonUtil jsonUtil = new JsonUtil();
        jsonUtil.setCode(0);
        jsonUtil.setData(list);
        return jsonUtil;
    }

    /**
     * 学生未读信息列表
     * @param teaNum
     * @return
     */
    @GetMapping("/stuSendMsg")
    public JsonUtil stuSendMsg(@RequestParam(value = "teaNum") String teaNum){
        System.out.println("---------------"+teaNum);

//        List< Student > list=studentService.list();
//        JsonUtil jsonUtil = new JsonUtil();
//        jsonUtil.setCode(0);
//        jsonUtil.setData(list);

        return studentService.stuSendMsgSer(teaNum);
    }

    /**
     * 学生的注册
     * 加上邮箱验证
     * @param regParam 注册人提交的信息
     * @return 注册的信息
     */
    @RequestMapping("/register")
    @ResponseBody
    public JsonUtil register(@RequestBody RegParam regParam){
        return studentService.register(regParam);
    }
//

    /**
     * 主页信息
     * @param stuNum
     * @return
     */
    @GetMapping("/stuMain")
    public JsonUtil stuMain(@RequestParam(value = "stuNum") String stuNum){
        return studentService.stuMainSer(stuNum);
    }

    /**
     * 修改个人信息
     * @param student
     * @return
     */
    @RequestMapping("/updateInfo")
    public JsonUtil updateInfo(@RequestBody Student student) {

        return studentService.updateStudentById(student);
    }

    /**
     * 修改个人信息的图片
     * @param  avatar 图片地址
     *                stuNum
     * @return 结果
     */
    @GetMapping("/updateImg")
    public JsonUtil updateStuImgUrl(@RequestParam(name = "avatar") String  avatar,
                                    @RequestParam(name = "stuNum") String  stuNum) {
        Student student = new Student();
        student.setAvatar(avatar);
        student.setStuNum(stuNum);
        return studentService.updateStuImgUrlSer(student);
    }


    /**
     * 修改头像
     * @param image 图片
     * @return url
     */
    @RequestMapping("/uploadImage")
    @ResponseBody
    public JsonUtil uploadImage(@RequestParam(name = "image") MultipartFile image){
        JsonUtil jsonUtil = new JsonUtil();
        String fileName = System.currentTimeMillis() + image.getOriginalFilename();//获取文件名
        File dir = new File("D:\\Tomcat\\apache-tomcat-9.0.46\\webapps\\photos");
        if (!dir.exists()){
            dir.mkdirs();
        }

        File targetFile = new File(dir, fileName);
        String url = "";
        try {
            image.transferTo(targetFile);
            url = "http://localhost:8089/photos/" + fileName;//记录图片url地址
            jsonUtil.setCode(0);
            jsonUtil.setData(url);
            jsonUtil.setMsg("上传成功");
        } catch (Exception e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("上传失败");
        }
        return jsonUtil;
    }



}

